package com.sgb.kjwl.viewmodel.house;


import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.lease.CompanyInfoEntity;
import com.sgb.kjwl.model.entity.lease.DictEntity4;
import com.sgb.kjwl.model.entity.lease.LeaseMapEntity;
import com.sgb.kjwl.model.entity.request.LeaseHallMapFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.entity.QualityMemberBean2;
import com.sgb.member.ui.activity.MemberInviteJoinActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.house.LeaseHallMapActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.im.immodel.IMUtils;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseHallMapModel extends BaseViewModel {
    private LeaseHallMapActivity activity;
    private MachineRepertory repertory;

    private int page = 1;
    private int size = 100;
    public int total;

    public LeaseHallMapModel(LeaseHallMapActivity activity, MachineRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public String getAreaName(){
        return repertory.getAreaName();
    }

    //获取类型
    public void getAllSrarchType() {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseMapAPI().getAllSrarchType().enqueue(new Callback<BaseEntity<List<DictEntity4>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity4>>> call, Response<BaseEntity<List<DictEntity4>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showTabList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity4>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    //获取一级类型
    public void getFilterTypeModel(String type) {
        APIManager.getInstance().getLeaseMapAPI().getFistcategory(type).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showTypePopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
            }
        });

    }

    //获取二级类型
    public void getFilterModelModel(String type, String parentNo) {
        APIManager.getInstance().getLeaseMapAPI().getSecondcategory(type, parentNo).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    activity.showBrandPopWindow(null);
                    return;
                }
                if (APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    activity.showBrandPopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
                activity.showBrandPopWindow(null);
            }
        });
    }

//    public void getCityData() {
//        APIManager.getInstance().getMachineAPI().getAreaSecondAndThird2(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Response<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    fragment.showAddressDatas(response.body().getData());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Throwable t) {
//
//            }
//        });
//    }

    /**
     * 获取省市区地址
     */
    public void getCityData() {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    activity.showAddressDatas(data, repertory.getAreaCode());
                }
            }
        });
    }

    //获取筛选列表
    public void getLeaseMapSearch(LeaseHallMapFilterJson filterJson, boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        filterJson.setCurrent(page);
        filterJson.setLimit(size);
        filterJson.setProvinceNo(repertory.getAreaCode());
        if (filterJson.getAreaNos() == null) {
            filterJson.setAreaNos(new ArrayList<>());
        }
//        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseMapAPI().getLeaseMapSearch(filterJson)
                .enqueue(new Callback<BaseEntity<ListResponse<List<LeaseMapEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse<List<LeaseMapEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseMapEntity>>>> response) {
//                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            activity.showMapMark(response.body().getData().getList());
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseMapEntity>>>> call, Throwable t) {
//                        DialogHelper.dismissProgressDialog();
                        t.printStackTrace();
                    }
                });
    }

    public void getH5Url(int pos, String id) {
        String name = "";
        if (pos == 1) {
            name = "mechanicsDetail";//机械
        } else if (pos == 2) {
            name = "equipmentDetails";//设备
        } else if (pos == 3) {
            name = "vehicleDetails";//车辆
        } else if (pos == 4) {
            name = "propertyDetails";//房产
        } else if (pos == 5) {
            name = "landDetails";//土地
        } else if (pos == 6) {
            name = "factoryDetails";//工厂
        } else if (pos == 7) {
            name = "farmDetail";//农机
        }

        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&iosFlag=1";
//                    WebViewX5HostActivity.start(activity, url);
                    WebViewX5HostActivity.start(activity, url, id, "lease", pos + "");
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void findCustomerServiceImAccount(String customerServiceContactsId) {
        DialogHelper.showProgressDialog(activity, "获取信息中...").setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("customerServiceContactsId", customerServiceContactsId);
        APIManager.getInstance().getPickAPI().getfindCustomerServiceImAccount(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    activity.checkAllowDialog(response.body().getMsg());
                } else {
                    String dataIMid = response.body().getData().toString();
                    IMUtils.getInstance().buildSingleTalking(dataIMid);
//                    NimUIKit.startP2PSession(activity, dataIMid);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    //通过企业no获取企业信息
    public void getCompany(String companyNo, String type) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseMapAPI().getCompanyImgByNo(companyNo, type).enqueue(new Callback<BaseEntity<CompanyInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<CompanyInfoEntity>> call, Response<BaseEntity<CompanyInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;
                if (APIManager.RESPONSE_OK.equals(response.body().getState()) && response.body().getData() != null) {
                    CompanyInfoEntity entity = response.body().getData();
                    QualityMemberBean memberBean = new QualityMemberBean();
                    memberBean.setCompanyId(entity.getId());
                    memberBean.setCompanyNo(companyNo);
                    memberBean.setCompanyName(entity.getName());
                    memberBean.setServiceType(entity.getServiceType());
                    memberBean.setServiceTypeName(entity.getServiceType());
                    memberBean.setAddress(entity.getAddress());
                    memberBean.setProvinceNo(entity.getProvinceNo());
                    memberBean.setCityNo(entity.getCityNo());
                    memberBean.setDistrictNo(entity.getDistrictNo());
                    MemberInviteJoinActivity.start(activity, memberBean);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CompanyInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void getProcessStatus(String companyNo, String approveType, QualityMemberBean memberBean) {
        NewAPIManager.getInstance().getCommonAPI().getProcessStatus(companyNo, approveType).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // "status": "1" //1 表示有审批 0 代表无审批
                String status = ((Map<String, String>) data.getData()).get("status");
                if (!TextUtils.isEmpty(status)) {
                    activity.updateProcessStatus(status, memberBean);
                } else {
                    MToast.showToast(activity, "网络异常，请稍后重试");
                }
            }
        });
    }
}
